using System.Collections;
using UnityEngine;

public class ButtonTrigger : TriggerBase
{
    [SerializeField] private SpriteRenderer spriteRenderer;
    [SerializeField] private int triggerCount = 1;
    private float _buttonSpriteHeight;
    private float heightPerCount;
    private int currentResidueCount;
    private Coroutine _coroutine;
    private ButtonTriggerManager _buttonTriggerManager;
    protected override void Awake()
    {
        base.Awake();
        _buttonTriggerManager = GetComponentInParent<ButtonTriggerManager>();
        _buttonSpriteHeight = spriteRenderer.bounds.size.y;
        heightPerCount = _buttonSpriteHeight / (triggerCount + 1);
        currentResidueCount = triggerCount;
    }
    protected override void OnTriggerEnter2D(Collider2D other)
    {
        base.OnTriggerEnter2D(other);
        if (currentResidueCount <= 0)
            return;

        if (other.CompareTag("PlayerBullet") == false)
            return;
        Vector2 startPos = transform.position;

        startPos.y -= heightPerCount;

        transform.position = startPos;

        Destroy(other.gameObject);

        currentResidueCount--;

        _buttonTriggerManager.ButtonTrigger();

        if (_coroutine != null)
            StopCoroutine(_coroutine);

        // _coroutine = StartCoroutine(WaitToRestoreCo());
    }

    // private IEnumerator WaitToRestoreCo()
    // {
    //     while (currentResidueCount < triggerCount)
    //     {
    //         yield return new WaitForSeconds(3f);

    //         Vector2 startPos = transform.position;

    //         startPos.y += heightPerCount;

    //         transform.position = startPos;

    //         currentResidueCount++;
    //     }
    // }
    public bool IsFinish() => currentResidueCount == 0;
}
